from rest_framework_jwt.views import ObtainJSONWebToken
from rest_framework.serializers import ValidationError
from utils import response_error, response_ok
from .serializers import UserJWTSerializer


class UserLoginView(ObtainJSONWebToken):
    serializer_class = UserJWTSerializer

    def post(self, request, *args, **kwargs):
        try:
            ser = self.serializer_class(data=request.data, context={'request': request})
            if ser.is_valid(raise_exception=True):
                return response_ok(ser.validated_data)
        except ValidationError as e:
            return response_error(e.detail)
